package com.lanchetech.service.impl;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
import cn.binarywang.wx.miniapp.bean.WxMaSubscribeMessage;
import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
import com.google.common.collect.Lists;
import com.lanchetech.common.enums.ActivityEnum;
import com.lanchetech.entity.FlashSaleActivity;
import com.lanchetech.entity.FlashSaleSpu;
import com.lanchetech.service.MiniAppMessageService;
import me.chanjar.weixin.common.error.WxErrorException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;

@Service
public class MiniAppMessageServiceImpl implements MiniAppMessageService {
    private final Logger log = LoggerFactory.getLogger(this.getClass());

    @Value("${wx.miniapp.configs.appid}")
    private String appid;

    @Value("${wx.miniapp.configs.secret}")
    private String secret;

    @Value("${wx.miniapp.configs.token}")
    private String token;

    @Value("${wx.miniapp.configs.aesKey}")
    private String aesKey;

    @Value("${wx.miniapp.configs.msgDataFormat}")
    private String msgDataFormat;

    public String getString(String str) {
        if (str.length() > 19) {
            String str1 = str.substring(0, 17) + "...";
            return str1;
        } else {
            return str;
        }
    }

    private WxMaService getMaService() {
        WxMaDefaultConfigImpl config = new WxMaDefaultConfigImpl();
        config.setAppid(this.appid);
        config.setSecret(this.secret);
        config.setToken(this.token);
        config.setAesKey(this.aesKey);
        config.setMsgDataFormat(this.msgDataFormat);
        WxMaService service = new WxMaServiceImpl();
        service.setWxMaConfig(config);
        return service;
    }

    @Override
    public void sendFlashSaleSubscribeMessage(String openid, FlashSaleActivity flashSaleActivity, FlashSaleSpu flashSaleSpu, BigDecimal originalPrice) {
        final WxMaService wxService = getMaService();
        try {
//            秒杀商品
//            秒杀价
//            商品原价
//            开始时间
//            温馨提示

            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            wxService.getMsgService().sendSubscribeMsg(WxMaSubscribeMessage.builder()
                    .templateId("HcSaFP7kpi58OnSVVMaw5C2Pkhj-yWz2uLOlD3puOiY")
                    .data(Lists.newArrayList(
                            new WxMaSubscribeMessage.Data("thing1", getString(flashSaleSpu.getSpuName())),
                            new WxMaSubscribeMessage.Data("amount2", flashSaleSpu.getMinPrice().toString()),
                            new WxMaSubscribeMessage.Data("amount3", originalPrice.toString()),
                            new WxMaSubscribeMessage.Data("time4", dateFormat.format(flashSaleActivity.getStartTime())),
                            new WxMaSubscribeMessage.Data("thing5", "活动十分钟后开启,点击立即参与")
                    ))
                    .toUser(openid)
                    // "/pages/home/productDetail?spuId=商品编号&activityType=活动类型&activityId=活动编号"
                    .page("/pages/home/productDetail?spuId="+flashSaleSpu.getSpuId()+"&activityType="+ ActivityEnum.FLASH_SALE.getActivity() +"&activityId="+flashSaleActivity.getId())
                    .build()
            );
            log.info("微信订阅跳转地址为{}","/pages/home/productDetail?spuId="+flashSaleSpu.getSpuId()+"&activityType="+ ActivityEnum.FLASH_SALE.getActivity() +"&activityId="+flashSaleActivity.getId());
        } catch (WxErrorException e) {
            e.printStackTrace();
        }
    }
}
